package com.yfuxdeer.youchat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.adapter.SearchFriendListAdapter;
import com.yfuxdeer.youchat.pojo.User;
import com.yfuxdeer.youchat.service.FriendService;
import com.yfuxdeer.youchat.service.UserService;
import com.yfuxdeer.youchat.utils.Store;

import java.util.ArrayList;
import java.util.List;

public class AddFriendActivity extends AppCompatActivity {

    private EditText friend_info;
    private Button search;
    private Button cancel;
    private Store store = Store.getInstance();
    private ListView listView;
    private SearchFriendListAdapter adapter;
    private List<User> notFriendList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        //注册广播
        registerReceiver(searchReceiver, new IntentFilter("com.yfuxdeer.youchat.searchReceiver"));

        findViews();
        init();
        setListeners();
    }

    private void findViews() {
        friend_info = findViewById(R.id.friend_info);
        search = findViewById(R.id.search);
        listView = findViewById(R.id.list);
        cancel = findViewById(R.id.cancel);
    }

    private void init() {
        adapter = new SearchFriendListAdapter(notFriendList, getApplicationContext());
        listView.setAdapter(adapter);
    }

    private void setListeners() {
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String info = friend_info.getText().toString();
                if (info.equals("")) {
                    Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                //调用服务 查询用户
                Intent intent = new Intent(getApplicationContext(), UserService.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("method", UserService.METHOD.SERACH);
                bundle.putString("username", store.getCurrentUser().getUsername());
                bundle.putString("info", info);
                intent.putExtras(bundle);
                startService(intent);
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private BroadcastReceiver searchReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            UserService.RESULT result = (UserService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    List<User> users = (List<User>) bundle.getSerializable("users");
                    if (users.isEmpty()) {
                        Toast.makeText(getApplicationContext(), "未查询到该用户", Toast.LENGTH_SHORT).show();
                    } else {
                        notFriendList.clear();
                        notFriendList.addAll(users);
                        adapter.notifyDataSetChanged();
                        Toast.makeText(getApplicationContext(), "搜索到" + users.size() + "条结果", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(getApplicationContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}